//给一个长度为n链表，若其中包含环，请找出该链表的环的入口结点，否则，返回null。
import java.util.*;

public class Solution {
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        ListNode node = hasCycle(pHead);
        if (node == null){
            return null;
        }
        ListNode tmp = pHead;
        while( node != tmp){
            node = node.next;
            tmp = tmp.next;
        }
        return node;
    }
    public ListNode hasCycle(ListNode pHead){
        if (pHead == null){
            return null;
        }
        ListNode fast = pHead;
        ListNode slow = pHead;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast){
                return slow;
            }
        }
        return null;
    }




}
